﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;

/// <summary>
/// AjaxHelper 的摘要描述
/// </summary>
public class AjaxHelper
{
    public AjaxHelper()
    { 
    }

    public static void WriteAjaxError(string messageKey, Exception e)
    {
        string message = string.Empty;
        //if (e is FDC.Core.FDCAccessDeniedException)
        //{
        //    message = WebUtility.GetMessage("msgAccessDenied", "Access Denied!!");
        //}
        //else if (e is FDC.Core.FDCDefineException)
        //{
        //    string msg = messageKey;
        //    msg = WebUtility.GetMessage(msg, e.Message, true);
        //    message = (string.IsNullOrEmpty(msg) ? WebUtility.GetMessage(messageKey, e.Message) : msg);
        //}
        //else
        //{ message = WebUtility.GetMessage(messageKey, e.Message); }
        message = WebUtility.GetMessage(messageKey, e.Message);

        
        WriteAjaxResult(GenAjaxResult(false, message, new ExceptionObject(e)));
    }

    public static void WriteAjaxResult(AjaxResult result)
    {
        HttpContext.Current.Response.Expires = 0;
        HttpContext.Current.Response.CacheControl = "no-cache";
        HttpContext.Current.Response.AddHeader("Pragma", "no-cache");
        HttpContext.Current.Response.Write(JsonConvert.SerializeObject(result));
    }

    public static void WriteAjaxResult(bool success, string messageKey, object data)
    {
        string message = WebUtility.GetMessage(messageKey, null);
        WriteAjaxResult(GenAjaxResult(success, message, data));
    }

    private static AjaxResult GenAjaxResult(bool success, string message, object data)
    {
        AjaxResult result = new AjaxResult();
        result.Success = success;
        result.Message = message;
        result.Data = data;
        return result;
    }
}